package com.liang.leetcode.dp;

import java.util.Scanner;

public class Dp_BagProblem {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int bagWeight = in.nextInt();
        int[] weight = new int[n];
        int[] value = new int[n];
        for (int i = 0; i < n; i++) {
            weight[i] = in.nextInt();
            value[i] = in.nextInt();
        }
        System.out.println(getMaxValue(n, bagWeight, weight, value));

    }

    public static int getMaxValue(int n, int bagWeight, int[] weight, int[] value) {
        int[][] dp = new int[n][bagWeight + 1];
        // 初始化
        for (int i = weight[0]; i < n; i++) {
            dp[0][i] = value[0];
        }
        //遍历物品 i
        for (int i = 1; i < n; i++) {
            // 遍历背包容量 j
            for (int j = 0; j <= bagWeight; j++) {
                // 装不下
                if (j < weight[i]) {
                    dp[i][j] = dp[i - 1][j];
                } else {
                    // 装得下
                    dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - weight[i]] + value[i]);
                }

            }
        }
        return dp[n - 1][bagWeight];
    }
}